/**
 * Created by lvlq on 16/1/7.
 */
var rule = require("./ruleController");
var session = require("./sessionController");
var model = require("./modelController");
var home = require("./homeController");
var main = require("./mainController");
var share = require("./shareController");
var contact = require("./contactController");
var toplist = require("./toplistController");
var exportfile = require("./exportController");
var api = Util("wx.api");

module.exports = function (app) {
    app.get("/exportfile", exportfile.exportCSV);


    app.get("/setsession", session.index);
    app.post("/setsession", session.indexpost);

    // app.get("*", function (req, res, next) {
    //     if (req.session.debug) {
    //         next();
    //     } else {
    //         res.send("系统维护,请稍后再试~~~");
    //     }
    // });
    app.get("*", function (req, res, next) {
        res.header("Cache-Control", "no-cache, no-store, must-revalidate");
        res.header("Pragma", "no-cache");
        res.header("Expires", 0);
        // var flag = false;
        // if (req.originalUrl.split("share").length > 1 && !req.query.oid) {
        //     flag = true
        // }
        //
        // if (!req.query.openid || flag) {
        //     var n = encodeURIComponent(req.originalUrl);
        //     var callback = "http://scgbg.weixinzjit.com/index/setsession?next=" + n;
        //     var oauthUrl = "http://api.weixinzjit.com/api.php/wx/oauth?callback=" + encodeURIComponent(callback);
        //     oauthUrl += "&type=userinfo";
        //     res.redirect(oauthUrl);
        // } else {
        //     var openid = req.query.openid || req.query.oid;
        //     var User = M("index.user");
        //     User.find({
        //         where: {
        //             openid: openid
        //         }
        //     }).then(function (u) {
        var param = {
            debug: true,
            jsApiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'onMenuShareQZone',
                'hideMenuItems',
                'showMenuItems',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'translateVoice',
                'startRecord',
                'stopRecord',
                'onVoiceRecordEnd',
                'playVoice',
                'onVoicePlayEnd',
                'pauseVoice',
                'stopVoice',
                'uploadVoice',
                'downloadVoice',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'getNetworkType',
                'openLocation',
                'getLocation',
                'hideOptionMenu',
                'showOptionMenu',
                'closeWindow',
                'scanQRCode',
                'chooseWXPay',
                'openProductSpecificView',
                'addCard',
                'chooseCard',
                'openCard'
            ],
            url: ("http://scgbg.weixinzjit.com" + req.originalUrl).split("#")[0]
        };

        api.getJsConfig(param, function (err, result) {
            if (!!err) {
            }

            res.locals.wxconfig = result;
            res.locals.title = "好伴二重奏 解锁购物车";
            res.locals.share = {};
            res.locals.share.link = "http://scgbg.weixinzjit.com/index/menu";
            res.locals.share.title = "好伴二重奏 解锁购物车";
            res.locals.share.img = "http://scgbg.weixinzjit.com/images/1.jpg";
            res.locals.share.desc = "渣打银行信用卡《好伴二重奏，解锁购物车》公布获奖名单啦";
            res.locals.share.qtitle = "渣打银行信用卡《好伴二重奏，解锁购物车》公布获奖名单啦";
            next()
        });


        // });
        // }
    });
    app.get("/rule", rule.index);
    app.get("/menu", toplist.menu);
    app.get("/toplist", toplist.index);
    app.get("*", home.doall);
    app.get("/home", home.index);
    app.get("/main", main.index);
    app.get("/share", share.index);
    app.get("/contact", contact.index);


    app.post("/share/addzan", share.addZan);
    app.post("/contact/saveinfo", contact.saveinfo);

};